//
//  ProductContentViewLightMaker.m
//  nonghubao
//
//  Created by 王 文彬 on 13-6-6.
//  Copyright (c) 2013年 Chongqing Nonghubao Co., Ltd. All rights reserved.
//

#import "ProductContentViewLightMaker.h"
#import "RTLabel.h"
#import "Config.h"
#import "DynamicPicturesView.h"
#import "DynamicFooterView.h"

#define PRODUCT_NAME_TEMPLATE @"名称：<font color='#86b60e'>%@</font>"
#define PRODUCT_PRICE_TEMPLATE @"价格：<font color='#86b60e'>%.2f元/%@</font>"

@implementation ProductContentViewLightMaker

+ (void)makePrototype:(UIView *)view
{
    //除商品名称和footer外，其余都没有真正的Y坐标，且高度都为0，且都为隐藏
    if (view) {
        //商品名Label
        RTLabel* productNameLabel = [[RTLabel alloc] initWithFrame:CGRectMake(10, 5, 280, 20)];
        productNameLabel.font = _SYS_FONT_002;
        productNameLabel.backgroundColor = [UIColor clearColor];
        [view addSubview:productNameLabel];
        
        //商品价格
        RTLabel* priceLabel = [[RTLabel alloc] initWithFrame:CGRectMake(10, 25, 280, 0)];
        priceLabel.font = _SYS_FONT_002;
        priceLabel.backgroundColor = [UIColor clearColor];
        priceLabel.hidden = true;
        [view addSubview:priceLabel];
        
        //商品产地
        UILabel* originLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 25, 280, 0)];
        originLabel.font = _SYS_FONT_002;
        originLabel.backgroundColor = [UIColor clearColor];
        originLabel.numberOfLines = 0;
        originLabel.lineBreakMode = UILineBreakModeWordWrap;
        originLabel.hidden = true;
        [view addSubview:originLabel];
        
        //商品图片
        UIView* picturesView = [[DynamicPicturesView alloc] initWithFrame:CGRectMake(10, 25, 290, 0)];
        picturesView.backgroundColor = [UIColor clearColor];
        picturesView.hidden = true;
        [view addSubview:picturesView];
    }
}


// 计算产品动态的高度
// 高度为：商品名称 + 销售区域 + 商品价格 + 图片
+ (float)heightWithDictionary:(NSDictionary *)data
{
    //商品名称一定会有，如果商品是已删除，那么名字那里就会显示已删除
    float height = 20.0f;

    //取出商品对象
    NSDictionary* product = data[@"product"];
    
    //商品
    if (![product isKindOfClass:[NSNull class]] && [product[@"status"] intValue] == 0) {
        //价格 + 产地
        height += 20 + 20;
        
        //获得产地的值
        NSString* originPlace = [NSString stringWithFormat:@"产地：%@", product[@"provenance"]];
        CGSize originPlaceSize = [originPlace sizeWithFont:_SYS_FONT_002 constrainedToSize:CGSizeMake(290.0f, 200.0f) lineBreakMode:UILineBreakModeWordWrap];
        height += originPlaceSize.height;
        
        //获得图片数组，并判断是否存在图片
        NSArray *pictures = product[@"pictures"];
        
        //有图片，且图片对象不为Null，且图片数量>0
        if (pictures != nil && ![pictures isKindOfClass:[NSNull class]] && pictures.count > 0) {
            height += _PICTURE_NORMAL_PREVIEW_HEIGHT + 5 + 13;
        }
    }
    
    return height;
}


// 根据数据重新绘制动态内容
+ (void)drawContentView:(UIView *)view withDictionary:(NSDictionary *)data
{
    
    //Y坐标的偏移，每多一个元素，就会偏移，用于定位全部元素的Y起点。从25开始，也就是从商品名称之后开始。25=5间隙+20Label高度。
    float offsetY = 25.0f;
    
    //取出商品对象
    NSDictionary* product = data[@"product"];
    
    //商品名称
    RTLabel* productNameLabel = view.subviews[0];
    if ([product isKindOfClass:[NSNull class]] || [product[@"status"] intValue] != 0) {
        productNameLabel.text = @"该商品已停售";
        
        //重置对象：第一个（商品名称）和最后一个不隐藏
        for (int i = 1; i < view.subviews.count - 1; i++) {
            ((UIView *)view.subviews[i]).hidden = true;
        }
    }
    else {
        productNameLabel.text = [NSString stringWithFormat:PRODUCT_NAME_TEMPLATE, product[@"name"]];
        
        //价格
        RTLabel* priceLabel = view.subviews[1];
        priceLabel.text = [NSString stringWithFormat:PRODUCT_PRICE_TEMPLATE, [product[@"price"] floatValue], product[@"unitName"]];
        priceLabel.hidden = false;
        priceLabel.frame = CGRectMake(productNameLabel.frame.origin.x, productNameLabel.frame.origin.y + productNameLabel.frame.size.height, 280, 20);
        
        //商品产地
        NSString* originPlace = product[@"provenance"] == nil ? @"" : product[@"provenance"];
        UILabel* originLabel = view.subviews[2];
        originLabel.text = [NSString stringWithFormat:@"产地：%@", originPlace];
        originLabel.hidden = false;
        CGSize originPlaceSize = [originPlace sizeWithFont:_SYS_FONT_002 constrainedToSize:CGSizeMake(290.0f, 200.0f) lineBreakMode:UILineBreakModeWordWrap];
        originLabel.frame = CGRectMake(priceLabel.frame.origin.x, priceLabel.frame.origin.y + priceLabel.frame.size.height, 280, originPlaceSize.height);
        
        //图片
        offsetY += priceLabel.frame.size.height + originPlaceSize.height + 13;  //图片间距多13
        DynamicPicturesView* picturesView = view.subviews[3];
        
        NSArray* pictures = product[@"pictures"];
        if (pictures != nil && ![pictures isKindOfClass:[NSNull class]] && pictures.count > 0) {
            //重画图片
            picturesView.hidden = false;
            picturesView.frame = CGRectMake(picturesView.frame.origin.x, offsetY, picturesView.frame.size.width, _PICTURE_NORMAL_PREVIEW_HEIGHT);
            [picturesView drawPictures:pictures];
            
            offsetY += _PICTURE_NORMAL_PREVIEW_HEIGHT ;
        }
        else {
            [picturesView drawPictures:nil];
        }
    }
    
    //重绘自己视图的|高度|
    view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, offsetY);
}

@end
